package com.zhibang.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration
public class RedisConfig {
    @Bean
    public RedisTemplate<String,Object> redisTemplate(RedisConnectionFactory rcf){
        RedisTemplate<String,Object> rt = new RedisTemplate<>();
        rt.setConnectionFactory(rcf);

        // 创建序列化器
        StringRedisSerializer str = new StringRedisSerializer();
        GenericJackson2JsonRedisSerializer jack = new GenericJackson2JsonRedisSerializer();
        // 配置序列化器
        rt.setKeySerializer(str);
        rt.setHashKeySerializer(str);

        rt.setValueSerializer(jack);
        rt.setHashValueSerializer(jack);
        return rt;
    }
}
